﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace iPick.Business
{
    public class ApplicationModelHelper
    {
        public static  iPick.Models.Application getModelPopulated(iPick.Models.Application ApplicationModel)
        {

          
            iPick.Controllers.DataServiceController DataService = new Controllers.DataServiceController();

            try
            {
                if (ApplicationModel == null)
                    ApplicationModel = new Models.Application();

                ApplicationModel.lCatalog = DataService.GetCatalogsByDeviceKey(ApplicationModel.CustomerDevice);
               // ApplicationModel.CustomerDevice = DataService.GetCustomerDeviceByDeviceKey(ApplicationModel.CustomerDevice.DeviceKey);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                DataService = null;
            }
            return ApplicationModel;
        }

        public static void SetSession()
        {
            if (HttpContext.Current.Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] == null)
            {
                iPick.Models.Application ApplicationModel = null;
                iPick.Controllers.DataServiceController DataService = new iPick.Controllers.DataServiceController();
                string deviceKey = "Device-Key";
                string devicename = "";
                deviceKey = "Device-Test3-Key";
                ApplicationModel = new Models.Application();

                if (!String.IsNullOrWhiteSpace(HttpContext.Current.Request["dkey"]))
                {
                    //ApplicationModel.CustomerDevice.DeviceKey = Request["dkey"];
                    deviceKey = HttpContext.Current.Request["dkey"];
                }

                if (!String.IsNullOrWhiteSpace(HttpContext.Current.Request["dname"]))
                {
                    //ApplicationModel.CustomerDevice.DeviceName = Request["dname"];
                    devicename = HttpContext.Current.Request["dname"];
                }
                // Only for Test->  deviceKey = "Device-Test3-Key";

                //TODO:We should get by Customer as well if the customer is logged in already, maybe local database

                ApplicationModel.CustomerDevice = DataService.GetCustomerDeviceByDeviceKey(deviceKey, devicename);

                ApplicationModel = iPick.Business.ApplicationModelHelper.getModelPopulated(ApplicationModel);
                HttpContext.Current.Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] = ApplicationModel;

            }


        }
       


        public static class Keys
        {

            public const string ApplicationModel = "ApplicationModel";
            public const string DeviceKey = "DeviceKey";

        }
    }
}